package djboom.materials.display.devices {
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.display.elements.SimpleSmartMovie;
	import djboom.materials.events.DesiccatorEvent;
	import djboom.materials.events.InstructionEvent;
	import djboom.materials.state.Font;

	import org.casalib.display.CasaBitmap;
	import org.casalib.display.CasaTextField;
	import org.casalib.util.FrameUtil;

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * Эксикатор.
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 01.02.2011
	 * 
	 */

	public class Desiccator extends SimpleSmartMovie {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const BACK:String = "desiccator.Back";

		private static const AUTO_ACTION_CLIP:String = "work3.AutoAction1";

		private static const AUTO_ACTION_LABEL_1:String = "phase1";		private static const AUTO_ACTION_LABEL_2:String = "phase2";

		private static const TFORMAT:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 13, 0x323232, true, null, null, null, null, "center", null, null, null, -2);

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _backBitmap:CasaBitmap;		 
		private var _textField:CasaTextField;
		private var _autoActionClip:MovieClip;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function Desiccator(materialName:String) {
			super();
			
			_backBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BACK));
			addChild(_backBitmap);
			
			_textField = new CasaTextField();
			_textField.embedFonts = true;
			_textField.selectable = false;
			_textField.selectable = false;
			_textField.multiline = true;
			_textField.wordWrap = true;
			_textField.defaultTextFormat = TFORMAT;
			_textField.autoSize = TextFieldAutoSize.CENTER;
			_textField.antiAliasType = AntiAliasType.ADVANCED;
			_textField.text = materialName.toLowerCase() + " ПОРОШОК";
			//			_textField.thickness = 50;
			_textField.width = 85;
			_textField.mouseEnabled = false;
			_textField.x = (_backBitmap.width - 85) * 0.5;			_textField.y = (_backBitmap.height - _textField.textHeight) * 0.5 - 1;
			_textField.filters = [new DropShadowFilter(1, 90, 0xffffff, 0.8, 1, 1)];
			addChild(_textField);
		}	 

		// ----------------------------------------------------------------------------
		// Приватные методы класса.
		// ----------------------------------------------------------------------------

		private function animateComplete():void {
			FrameUtil.removeFrameScript(_autoActionClip, BitmapDataCore.LABEL_COMPLETE);
            
			_autoActionClip.stop();
			_autoActionClip.visible = false;
			removeChild(_autoActionClip);
			_autoActionClip = null;
			
			dispatchEvent(new InstructionEvent(InstructionEvent.REFRESH));
		}

		private function spoon1():void {
			FrameUtil.removeFrameScript(_autoActionClip, AUTO_ACTION_LABEL_1);
			 
			dispatchEvent(new DesiccatorEvent(DesiccatorEvent.SPOON_1));
		}

		private function spoon2():void {
			FrameUtil.removeFrameScript(_autoActionClip, AUTO_ACTION_LABEL_2);
			 
			dispatchEvent(new DesiccatorEvent(DesiccatorEvent.SPOON_2));
		}

		
		// ----------------------------------------------------------------------------
		// Видимые методы класса.
		// ----------------------------------------------------------------------------
	
		// ----------------------------------------------------------------------------
		// Переопределенные методы.
		// ----------------------------------------------------------------------------

		override public function clearContent(event:Event):void {
			super.clearContent(event);
		}

		
		// ----------------------------------------------------------------------------
		// Обработчики событий.
		// ----------------------------------------------------------------------------
	
		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function animate():void {
			_autoActionClip = BitmapDataCore.getMovieClip(AUTO_ACTION_CLIP);
			addChildAt(_autoActionClip, 0);
			
			FrameUtil.addFrameScript(_autoActionClip, BitmapDataCore.LABEL_COMPLETE, animateComplete);			FrameUtil.addFrameScript(_autoActionClip, AUTO_ACTION_LABEL_1, spoon1);			FrameUtil.addFrameScript(_autoActionClip, AUTO_ACTION_LABEL_2, spoon2);
		}

		// ----------------------------------------------------------------------------
		// Методы доступа.
		// ----------------------------------------------------------------------------
	}
}
